<?php
namespace Admin\Controller;

class CategoryArticleController extends CommonController {
    private $articles;
    private $categories;
    public function __construct()
    {
        parent::__construct();
        $this->articles = M('article');
        $this->categories = M('category');
    }

    /**
     * 文章列表
     */
    public function article_list()
    {
        $Article=D('Article');
        $category_id=I('get.id');
        $count      = $Article->where("category_id='$category_id'")->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(5)

        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');

        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $Article->relation(true)->where("category_id='$category_id'")->order('time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
//        $articles=$Article->relation(true)->order('time desc')->select();
//        $this->assign('articles',$articles);
        $this->display();
    }

    /**
     * 增加文章
     */
    public function create_article()
    {
        $categories=$this->categories->where('parent_id=0')->order('sort_order')->select();
        foreach ($categories as $key => $value){
            $id=$value['id'];
            $children=$this->categories->where("parent_id='$id'")->order('sort_order')->select();
            $categories[$key]['children']=$children;
        }
        $this->assign('categories',$categories);
        $this->display();
    }

    /**
     * 提交文章
     */
    function store_article()
    {
        $this->articles->create();
        $time=I('post.time');
        $this->articles->time=strtotime($time);
        $this->articles->add();
        $this->success('添加成功',U('article_list'));
    }

    /**
     * 编辑文章
     */
    public function edit_article()
    {
        $categories=$this->categories->where('parent_id=0')->order('sort_order')->select();
        foreach ($categories as $key => $value){
            $id=$value['id'];
            $children=$this->categories->where("parent_id='$id'")->order('sort_order')->select();
            $categories[$key]['children']=$children;
        }
        $id=I('get.id');
        $this->assign('article',$this->articles->find($id));
        $this->assign('categories',$categories);
        $this->display();
    }

    /**
     * 更新文章
     */
    function update_article(){
        $this->articles->create();
        $this->articles->time=time();
        $this->articles->save();
        $this->success('编辑成功',U('article_list'));
    }

    /**
     * 删除文章
     */
    function destroy_article(){
        $id=I('get.id');
        $this->articles->delete($id);
        $this->success('删除成功');
    }

    /**
     * 删除选中
     */
    function destroy_checked(){
        $delete_id=I('post.delete_id');
        foreach ($delete_id as $id){
            $this->articles->delete($id);
        }
        $this->success('删除成功');
    }

//    /**
//     * 栏目列表
//     */
//    public function category_list()
//    {
//        $Category=D('Category');
//        if(!S('category')){
//            $categories=$Category->relation(true)->order('sort_order')->where('parent_id=0')->select();
//            S('category',$categories,10);}
//        $categories=S('category');
////        $categories=$this->categories->where('parent_id=0')->order('sort_order')->select();
////        foreach ($categories as $key => $value){
////            $id=$value['id'];
////            $children=$this->categories->where("parent_id='$id'")->order('sort_order')->select();
////            $categories[$key]['children']=$children;
////        }
//        $this->assign('categories',$categories);
//        $this->display();
//    }
//
//    /**
//     * 增加栏目
//     */
//    function create_category()
//    {
//        $categories=$this->categories->where('parent_id=0')->order('sort_order')->select();
//        $this->assign('categories',$categories);
//        $this->display();
//    }
//
//    /**
//     * 提交栏目
//     */
//    function store_category(){
//        $this->categories->create();
//        $this->categories->add();
//        $this->success('添加成功',U('category_list'));
//    }
//
//    /**
//     * 删除栏目
//     */
//    function destroy_category(){
//        $id=I('get.id');
//        $this->categories->delete($id);
//        $this->success('删除成功');
//    }
//
//    /**
//     * 栏目排序
//     */
//    function sort_order(){
//        S('category',NULL);
//        $sort_order = I('post.sort_order');
//        $id = I('post.id');
//        foreach ($id as $key => $value){
//            $this->categories->where("id=$value")->setField('sort_order',$sort_order[$key]);
//
//        }
//        $this->redirect('category_list');
//    }
//
//    /**
//     * 编辑栏目
//     */
//    public function edit_category()
//    {
//        $id=I('get.id');
//        $Category=D('Category');
//        $categories=$Category->relation(true)->order('sort_order')->where('parent_id=0')->select();
//        $this->assign('categories',$categories);
//        $this->assign('category',$this->categories->find($id));
//        $this->display();
//    }
//
//    /**
//     * 更新栏目
//     */
//    function update_category(){
//        $this->categories->create();
//        $this->categories->save();
//        $this->success('编辑成功',U('category_list'));
//    }
}